package com.joysuch.wwyt.datadump.biz.processor;

import cn.zlg.common.datadump.annotation.DataDumpEntity;
import cn.zlg.common.datadump.processor.BaseImportEntityProcessor;
import com.alibaba.fastjson.JSONObject;
import com.joysuch.wwyt.datadump.biz.model.MajorHazardImportProcessAfterValidateMsgBean;
import com.joysuch.wwyt.datadump.biz.model.MhMajorHazardChemProductRelBean;
import com.joysuch.wwyt.datadump.biz.model.MhMajorHazardDumpBean;
import com.joysuch.wwyt.mh.entity.MhMajorHazard;
import com.joysuch.wwyt.mh.entity.MhMajorHazardChemProduct;
import com.joysuch.wwyt.mh.entity.MhMajorHazardChemProductRel;
import com.joysuch.wwyt.mh.enums.MajorHazardLevelEnum;
import com.joysuch.wwyt.mh.mapper.MhMajorHazardMapper;
import com.joysuch.wwyt.mh.repository.MhMajorHazardChemProductDao;
import com.joysuch.wwyt.mh.repository.MhMajorHazardChemProductRelDao;
import com.joysuch.wwyt.mh.repository.MhMajorHazardDao;
import org.apache.tomcat.util.buf.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Component
public class MajorHazardImportProcessor extends BaseImportEntityProcessor<MhMajorHazardDumpBean, MhMajorHazard> {

    @Autowired
    private MhMajorHazardChemProductRelDao relDao;

    @Autowired
    private MhMajorHazardChemProductDao productDao;

    @Autowired
    private MhMajorHazardMapper mhMajorHazardMapper;

    @Autowired
    private MhMajorHazardDao mhMajorHazardDao;

    @Override
    public MhMajorHazard processAfterSave(MhMajorHazardDumpBean origin, MhMajorHazard entity, DataDumpEntity annotation) {
        List<MhMajorHazardChemProductRelBean> chemList = origin.getChemProductRelBeanList();
        System.err.println(chemList);
        if (chemList == null || chemList.isEmpty()) {
            return entity;
        }
        List<String> errors = new ArrayList<>();
        for (MhMajorHazardChemProductRelBean chem : chemList) {
            if (chem.getCasNo() == null) {
                continue;
            }
            MhMajorHazardChemProduct product = productDao.findByCasNo(chem.getCasNo());
//            Assert.notNull(product, "危化品"+chem.getChemProductName()+"不存在");
            if (product == null) {
                errors.add("不存在CAS号为[" + chem.getCasNo() + "]的危化品");
                continue;
            }
            List<MhMajorHazardChemProductRel> relList = relDao.findIdByPidAndMid(product.getId(), entity.getId());
            MhMajorHazardChemProductRel rel = null;
            if (relList.isEmpty()) {
                rel = new MhMajorHazardChemProductRel();
                rel.setMajorHazardId(entity.getId());
                rel.setMhMajorHazardChemProduct(product);
                relDao.save(rel);
            } else {
                rel = relList.get(0);
            }
            rel.setChemProductFeature(chem.getChemProductFeature());
            rel.setCriticalStock(chem.getCriticalStock());
            rel.setDesignStock(chem.getDesignStock());
            rel.setFixRate(chem.getFixRate());
            rel.setOperationPressure(chem.getOperationPressure());
            rel.setOperationTemperature(chem.getOperationTemperature());
            rel.setStock(chem.getStock());
            rel.setUsage(chem.getUsage());
            rel.setSubstanceStatus(chem.getSubstanceStatus());
            relDao.save(rel);
        }
        if (errors.size() > 0) {
            throw new IllegalArgumentException(StringUtils.join(errors, '\n'));
        }
        return super.processAfterSave(origin, entity, annotation);
    }

    @Override
    public MhMajorHazardDumpBean processAftertValidate(MhMajorHazardDumpBean origin, DataDumpEntity annotation) {
        Double rvalue = origin.getRvalue();
        String level = origin.getLevel();
        MajorHazardImportProcessAfterValidateMsgBean msgBean = new MajorHazardImportProcessAfterValidateMsgBean();
        boolean rValueOk = true;
        String msg = "";
//        if (!org.springframework.util.StringUtils.isEmpty(origin.getName())){
//            List<MhMajorHazard> mList = mhMajorHazardDao.findByNameAndState(origin.getName());
//            if (!CollectionUtils.isEmpty(mList)) {
//                rValueOk = false;
//                msg = "危险源名称已存在";
//                msgBean.setFileName("危险源名称");
//                msgBean.setMsg(msg);
//            }
//        }
        if (rvalue != null) {
            if (MajorHazardLevelEnum.LEVEL_1.getLevel().equals(level) && rvalue < 100) {
                rValueOk = false;
                msg = "【" + level + "】危险源R值范围: R >= 100, 请正确填写";
            } else if (MajorHazardLevelEnum.LEVEL_2.getLevel().equals(level) && (rvalue < 50 || rvalue >= 100)) {
                rValueOk = false;
                msg = "【" + level + "】危险源R值范围: 100 > R >= 50, 请正确填写";
            } else if (MajorHazardLevelEnum.LEVEL_3.getLevel().equals(level) && (rvalue < 10 || rvalue >= 50)) {
                rValueOk = false;
                msg = "【" + level + "】危险源R值范围: 50 > R >= 10, 请正确填写";
            } else if (MajorHazardLevelEnum.LEVEL_4.getLevel().equals(level) && (rvalue <= 0 || rvalue >= 10)) {
                rValueOk = false;
                msg = "【" + level + "】危险源R值范围: 10 > R > 0, 请正确填写";
            } else if (MajorHazardLevelEnum.LEVEL_0.getLevel().equals(level) && rvalue > 0) {
                msg = "【" + level + "】危险源R值范围: R <= 0, 请正确填写";
                rValueOk = false;
            }
            msgBean.setFileName("R值");
            msgBean.setMsg(msg);
        }
        if (!rValueOk) {
            throw new IllegalArgumentException(JSONObject.toJSONString(msgBean));
        }
        return origin;
    }

}
